package com.yanqu.road.server.manager.activity.commonteam.activitytypehandler.handler;

import com.yanqu.road.entity.activity.commonteam.data.CommonTeamUserData;
import com.yanqu.road.entity.activity.youxing.CrossXingYouShanShuiTeamData;
import com.yanqu.road.entity.activity.youxing.CrossXingYouShanShuiUserData;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.log.cross.xingyou.LogCrossXingYouSlive;
import com.yanqu.road.exception.BusinessException;
import com.yanqu.road.pb.activity.CommonTeamProto;
import com.yanqu.road.pb.activity.XingYouShanShuiProto;
import com.yanqu.road.server.manager.activity.commonteam.CommonTeamActivity;
import com.yanqu.road.server.manager.activity.commonteam.CommonTeamMgr;
import com.yanqu.road.server.manager.activity.commonteam.activitytypehandler.CommonTeamActivityTypeStrategy;
import com.yanqu.road.server.manager.activity.xingyou.CrossXingYouActivity;
import com.yanqu.road.server.manager.activity.xingyou.CrossXingYouShanShuiMgr;
import com.yanqu.road.server.manager.log.CrossAutoLogMgr;
import com.yanqu.road.utils.FixSizeLinkedList;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class XingYouShanShuiStrategy implements CommonTeamActivityTypeStrategy {

    @Override
    public List<Long> getUserActivityScoreList(int activityId, int groupId, long userId) {
        List<Long> scoreList = new ArrayList<>();
        //获取活动
        CrossXingYouActivity activity = CrossXingYouShanShuiMgr.getActivity(activityId, groupId);
        if (activity == null) {
            scoreList.add(0L);
            return scoreList;
        }
        //获取玩家
        CrossXingYouShanShuiUserData userData = activity.getUserData(userId);
        if (userData == null) {
            scoreList.add(0L);
            return scoreList;
        }
        scoreList.add(userData.getSilverCoinTotal());
        return scoreList;
    }

    @Override
    public List<Integer> getTeamRankConditionTypes() {
        List<Integer> types = new ArrayList<>();
        types.add(eGamePlayerEventType.XingYouShanShuiTeamSliverRank.getValue());
        return types;
    }

    @Override
    public FixSizeLinkedList<CommonTeamProto.CommonTeamChatMsg> formatTeamChatMsgList(int activityId, int groupId, FixSizeLinkedList<CommonTeamProto.CommonTeamChatMsg> chatMsgList) {
        return chatMsgList;
    }

    @Override
    public void agreeJoinTeamAfterHandle(int activityId, int groupId, int teamId, CommonTeamUserData userData, String language) throws BusinessException {
        CrossXingYouActivity activity = CrossXingYouShanShuiMgr.getActivity(activityId, groupId);
        if(activity == null){
            throw new RuntimeException(String.format("行游活动 %d 未找到", activityId));
        }
        CrossXingYouShanShuiUserData shanShuiUserData = activity.getUserData(userData.getUserId());
        shanShuiUserData.setTeamId(teamId);
        shanShuiUserData.setRoleId(activity.randomRoleId(teamId));
        CommonTeamActivity teamActivity = CommonTeamMgr.getActivity(activityId, groupId);
        if(teamActivity != null){
            CommonTeamUserData teamUserData = teamActivity.getUserData(userData.getUserId());
            if(teamUserData != null){
                teamUserData.setExtendsParam(String.valueOf(shanShuiUserData.getRoleId()));
            }
        }
        CrossXingYouShanShuiTeamData teamData = activity.getTeamData(teamId);
        synchronized (activity.getTeamLocker(teamId)){
            if(teamData != null){
                teamData.setSilverCoin(teamData.getSilverCoin() + activity.getConfig().getSHANSHUI_START_SILVER_COIN());

                //如果不在组队期
                if(teamActivity != null && !teamActivity.checkInTeamTime()){
                    LogCrossXingYouSlive logCrossXingYouSlive = new LogCrossXingYouSlive(teamData.getTeamId(), teamData.getActivityId(), userData.getUserId(), 4, activity.getConfig().getSHANSHUI_START_SILVER_COIN(), teamData.getSilverCoin(), 5);
                    logCrossXingYouSlive.setGroupId(groupId);
                    CrossAutoLogMgr.add(logCrossXingYouSlive);
                }
            }
        }
        activity.syncShanShuiPropNotify(teamId, Collections.singletonList(XingYouShanShuiProto.PROP.TEAM_SILVER_VALUE));
    }

    @Override
    public void quitTeamAfterHandle(int activityId, int groupId, int teamId, long userId) throws BusinessException {
        CrossXingYouActivity activity = CrossXingYouShanShuiMgr.getActivity(activityId, groupId);
        if(activity == null){
            throw new RuntimeException(String.format("行游活动 %d 未找到", activityId));
        }
        CrossXingYouShanShuiUserData userData = activity.getUserData(userId);
        userData.setTeamId(0);
        userData.setRoleId(0);
        CommonTeamActivity teamActivity = CommonTeamMgr.getActivity(activityId, groupId);
        if(teamActivity != null){
            CommonTeamUserData teamUserData = teamActivity.getUserData(userData.getUserId());
            if(teamUserData != null){
                teamUserData.setExtendsParam("");
            }
        }
        CrossXingYouShanShuiTeamData teamData = activity.getTeamData(teamId);
        synchronized (activity.getTeamLocker(teamId)){
            if(teamData != null){
                teamData.setSilverCoin(teamData.getSilverCoin() - activity.getConfig().getSHANSHUI_START_SILVER_COIN());
            }
        }
        activity.syncShanShuiPropNotify(teamId, Collections.singletonList(XingYouShanShuiProto.PROP.TEAM_SILVER_VALUE));
    }

    @Override
    public void createTeamAfterHandle(int activityId, int groupId, int teamId, long userId) throws BusinessException {
        CrossXingYouActivity activity = CrossXingYouShanShuiMgr.getActivity(activityId, groupId);
        if(activity == null){
            throw new RuntimeException(String.format("行游活动 %d 未找到", activityId));
        }
        CrossXingYouShanShuiUserData userData = activity.getUserData(userId);
        userData.setTeamId(teamId);
        userData.setCreateTeamTimes(1 + userData.getCreateTeamTimes());
        userData.setRoleId(activity.randomRoleId(teamId));
        CommonTeamActivity teamActivity = CommonTeamMgr.getActivity(activityId, groupId);
        if(teamActivity != null){
            CommonTeamUserData teamUserData = teamActivity.getUserData(userId);
            if(teamUserData != null){
                teamUserData.setExtendsParam(String.valueOf(userData.getRoleId()));
            }
        }
        CrossXingYouShanShuiTeamData teamData = activity.getTeamData(teamId);
        synchronized (activity.getTeamLocker(teamId)){
            if(teamData != null){
                teamData.setSilverCoin(teamData.getSilverCoin() + activity.getConfig().getSHANSHUI_START_SILVER_COIN());
            }
        }
        activity.syncShanShuiPropNotify(teamId, Collections.singletonList(XingYouShanShuiProto.PROP.TEAM_SILVER_VALUE));
    }
}
